package com.pengtu.gsj.service.impl.check.gcjs;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.jsbz.RoadMaterialDao;
import com.pengtu.gsj.dao.system.AttachmentDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.jsbz.RoadMaterial;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.gcjs.RoadMatRecService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class RoadMatRecServiceImpl implements RoadMatRecService{
	
	@Autowired
	private AttachmentDao attachmentDao;
	@Autowired
	private SystemManageService systemManageService;
	@Autowired
	private RoadMaterialDao roadMaterialDao;
	@Autowired
	private CheckTableService checkTableService;	
	
	@Override
	public List<RoadMaterial> getRoadMaterials(QueryFilter queryFilter) {
		return roadMaterialDao.find(queryFilter);
	}
	
	@Override
	@Transactional(readOnly = true)
	public List<Attachment> getAttachmentList(QueryFilter fileter) {
		return attachmentDao.find(fileter);
	}

	@Override
	public List<RoadMaterial> getRoadMatRecYear(String startTime, String endTime) {
		return roadMaterialDao.getRoadMatRecYear(startTime, endTime);
	}

	@Override
	public Map<String, Object> getRoadMatRecs(String startTime, String endTime,	CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Object> values = new ArrayList<Object>();
		List<Attachment> list = new ArrayList<Attachment>();
		List<RoadMaterial> roadMaterials = new ArrayList<>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		String year = (String)jsonObject.get("secondString");
		double tableFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double reduceScore = Double.parseDouble((String)jsonObject.get("yearPart"));
		double fullScoreRatio = Double.parseDouble((String)jsonObject.get("poitMark"));
		double actUtileze = 0; 		// 统一回收利用率
		double total = 0;					//总分
		double percentage = 0;			//系统完善度
		double asphaltLayer = 0;
		double asphalMaterial = 0;
		double cementLayer = 0;
		double cementMaterial = 0;
		//根据时间、标签查询是否有附件
		list = systemManageService.getCheckAttachments(startTime, endTime, tag);
		if (StringUtils.isNotEmpty(year)) {
			//根据年份查询roadMaterial集合
			QueryFilter queryFilter = new QueryFilter();
			queryFilter.addFilter("testYear", year, MatchType.EQ);
			roadMaterials = roadMaterialDao.find(queryFilter);
			for (RoadMaterial roadMatRec : roadMaterials) {
				asphaltLayer += roadMatRec.getAsphaltLayer();
				asphalMaterial += roadMatRec.getAsphalMaterial();
				cementLayer += roadMatRec.getCementLayer();
				cementMaterial += roadMatRec.getCementMaterial();
				actUtileze = MathsUtils.divDouble((asphalMaterial + cementMaterial) * 100, asphaltLayer + cementLayer, 2);
			}
			//就地利用率与统一回收率算分：
			if (actUtileze >= fullScoreRatio) {
				total = tableFull;
			} else if (actUtileze >= 0 && actUtileze < fullScoreRatio) {
				//得分 = 总分 - 被扣分数
				total = tableFull - MathsUtils.mulDouble(reduceScore, MathsUtils.divDouble(10 * (fullScoreRatio - actUtileze), 3, 2));
			}
			//分数保留一位小数
			total = MathsUtils.formatDouble(total, 1);
			total = total <  0 ? 0 : total;
			//目标对象全部遍历后，可以得到总分total并算出完善度
			percentage = MathsUtils.divDouble(100 * total, tableFull, 2);
			//添加一个标记，确定前端是否输出表格
			map.put("flag",true);
			values.add(year);
		}
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, values);		
		map.put("att", list);
		map.put("testYear", year);
		map.put("asphaltLayer", asphaltLayer);
		map.put("asphalMaterial", asphalMaterial);
		map.put("cementLayer", cementLayer);
		map.put("cementMaterial", cementMaterial);
		map.put("actUtileze", actUtileze);
		map.put("percent", percentage);
		map.put("manageData", roadMaterials);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}
}
